//*****************************************************************************
//
// Copyright, (C) 2000-2005 Quantic Dream SA
//
// These coded instructions, statements and computer programs contain
// unpublished information, proprietary to Quantic Dream SA and are 
// protected by French and EEC copyright laws. They may not be 
// disclosed to third parties or copied or duplicated, in whole or in part, 
// without prior written consent of Quantic Dream SA
//
// Unpublished-rights reserved under the Copyright Laws of the EEC.
//
//*****************************************************************************
//
//	CLASS:	GOURAUD_SHADER
//
//	JJA : 2012-07-23 - Created
//*****************************************************************************

#include "GouraudShader.h"
#include "Mesh/Mesh.h"
#include "Mesh/MeshInstance.h"
#include "Camera/Camera.h"

//=============================================================================
//	CODE STARTS HERE
//=============================================================================

//-----------------------------------------------------------------------------
//! @brief		GOURAUD_SHADER constructor
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-23
//-----------------------------------------------------------------------------
GOURAUD_SHADER::GOURAUD_SHADER()
: _uiModelViewMatrixUniformLocation		( GL_INVALID_INDEX )
, _uiProjectionMatrixUniformLocation	( GL_INVALID_INDEX )
, _uiLightDirInViewSpaceUniformLocation	( GL_INVALID_INDEX )
{
	
}

//-----------------------------------------------------------------------------
//! @brief		GOURAUD_SHADER destructor
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-23
//-----------------------------------------------------------------------------
GOURAUD_SHADER::~GOURAUD_SHADER()
{
	
}

//-----------------------------------------------------------------------------
//! @brief		Load
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-25
//-----------------------------------------------------------------------------
void	GOURAUD_SHADER::Load(const char *	pcVertexShaderPath,
							 const char *	pcFragmentShaderPath)
{
	SHADER::Load( pcVertexShaderPath, pcFragmentShaderPath );

	_uiModelViewMatrixUniformLocation		= glGetUniformLocation( _uiShaderProgramId, "mModelViewMatrix" );
	_uiProjectionMatrixUniformLocation		= glGetUniformLocation( _uiShaderProgramId, "mProjectionMatrix" );
	_uiLightDirInViewSpaceUniformLocation	= glGetUniformLocation( _uiShaderProgramId, "vLightDirInCamSpace" );
}

//-----------------------------------------------------------------------------
//! @brief		Begin
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-26
//-----------------------------------------------------------------------------
void	GOURAUD_SHADER::Begin() const
{
	glUseProgram( _uiShaderProgramId );

	glEnableVertexAttribArray( VERTEX_ATTRIB_POSITION );
	glEnableVertexAttribArray( VERTEX_ATTRIB_COLOR );
	glEnableVertexAttribArray( VERTEX_ATTRIB_NORMAL );
}

//-----------------------------------------------------------------------------
//! @brief		Apply
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-26
//-----------------------------------------------------------------------------
void	GOURAUD_SHADER::Apply(MESH_INSTANCE *	pMeshInstance, 
							  CAMERA *			pCamera) const
{
	const MATRIX4&	mModelMatrix		= pMeshInstance->GetWorldMatrix();
	const MATRIX4&	mViewMatrix			= pCamera->GetWorldMatrix();
	const MATRIX4&	mProjectionMatrix	= pCamera->GetProjectionMatrix();
	MATRIX4			mModelView			= mViewMatrix * mModelMatrix;
	VECTOR4			vLightDir( 1.0f, -1.0f, 0.0f, 0.0f );
	VECTOR4			vLightDirInCamSpace = mViewMatrix * -vLightDir;

	glUniformMatrix4fv( _uiModelViewMatrixUniformLocation, 1, GL_FALSE, (GLfloat*)&mModelView );
	glUniformMatrix4fv( _uiProjectionMatrixUniformLocation, 1, GL_FALSE, (GLfloat*)&mProjectionMatrix );
	glUniform4fv( _uiLightDirInViewSpaceUniformLocation, 1, (GLfloat*)&vLightDirInCamSpace );
}

//-----------------------------------------------------------------------------
//! @brief		End
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-26
//-----------------------------------------------------------------------------
void	GOURAUD_SHADER::End() const
{
	glDisableVertexAttribArray( VERTEX_ATTRIB_POSITION );
	glDisableVertexAttribArray( VERTEX_ATTRIB_COLOR );
	glDisableVertexAttribArray( VERTEX_ATTRIB_NORMAL );

	glUseProgram( 0 );
}

//=============================================================================
//	CODE ENDS HERE
//=============================================================================
